import com.mysql.cj.jdbc.Driver;
import org.junit.Test;

import java.io.IOException;
import java.sql.*;
public class javaLean {
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        javaLean javaLean = new javaLean();
//        javaLean.add();
//        javaLean.select();
//        javaLean.delete();
//        javaLean.update();
    }
    private String user;
    private String password;
    private String db;
    private Connection connection;

    public javaLean(){

    }
    public javaLean(String user,String password,String db){
        this.user=user;
        this.password=password;
        this.db=db;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            //获取连接
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/" +
                    ""+db+"?useSSL=false&serverTimezone=UTC", this.user, this.password);
//            new JdbcLearn();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
//        } catch (IOException ioException) {
//            ioException.printStackTrace();
        }
    }


    @Test
  public void add(String sql) throws ClassNotFoundException, SQLException {
      //创建数据库操作对象
         new javaLean();
        PreparedStatement statement = connection.prepareStatement(sql);
      //执行sql,获取结果集
        statement.setString(1,"网络编程");
        statement.setDouble(2,3.5);
      int i = statement.executeUpdate();
//        Statement statement = connection.createStatement();
//        ResultSet resultSet = statement.executeQuery(sql);
      System.out.println("数据库添加了 " + i+"数据");
      //关闭资源
      statement.close();
      connection.close();
  }
// @Test
//    public void add() throws ClassNotFoundException, SQLException {
//        new javaLean();
//        String sql="insert into major values (null,'信息管理系统')";
//        Statement statement = connection.createStatement();
//        int i = statement.executeUpdate(sql);
//        System.out.println("数据库添加了 " + i+"数据");
//        statement.close();
//        connection.close();
//    }
@Test
    public void update(String sql) throws ClassNotFoundException, SQLException {
        new javaLean();
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1,"C语言");
        statement.setInt(2,1007);
        int i = statement.executeUpdate();
//    Statement statement = connection.createStatement();
//    ResultSet resultSet = statement.executeQuery(sql);
        System.out.println("数据库修改了 " + i+"数据");
        statement.close();
        connection.close();
    }
    @Test
    public void delete(String sql) throws ClassNotFoundException, SQLException {
        //加载驱动
      new javaLean();
        //定义语句
        //创建数据库操作对象
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1,1006);
        //执行sql,获取结果集
        int i = statement.executeUpdate();
        System.out.println("数据库删除了 " + i+"数据");
        //关闭资源
//        Statement statement = connection.createStatement();
//        ResultSet resultSet = statement.executeQuery(sql);
        statement.close();
        connection.close();
    }


    @Test
    public void select(String sql) throws ClassNotFoundException, SQLException {
        new javaLean();
        PreparedStatement statement = connection.prepareStatement(sql);
        ResultSet resultSet = statement.executeQuery();
//        Statement statement = connection.createStatement();
//        ResultSet resultSet = statement.executeQuery(sql);
        while (resultSet.next()) {
            System.out.println("id:" + resultSet.getInt("id"));
            System.out.println("course:" + resultSet.getString("course"));
            System.out.println("score: " + resultSet.getDouble("score"));
        }
        statement.close();
        connection.close();
    }
}
